import { _decorator, Component, Node, Input, EventTouch, Vec2, Vec3, UITransform, tween } from "cc"
import DataManager from "../Runtime/DataManager"
import EventManager from "../Runtime/EventManager"
import { DIRECTION_ENUM, GAME_MODE_ENUM, PLAYER_ENUM, SOCKET_TYPE } from "../../Enums"
import { sendSocket } from "../service/socket"
import { userStore } from "../service/user"
const { ccclass } = _decorator

@ccclass("JoyStickManager")
export class JoyStickManager extends Component {
  input: Vec2 = Vec2.ZERO

  private body: Node
  private stick: Node
  private status: string
  private defaultPos: Vec2
  private radius: number = 0
  private callback
  private timeout = null
  init() {
    this.body = this.node.getChildByName("Body")
    this.stick = this.body.getChildByName("Stick")
    const { x, y } = this.body.position
    this.defaultPos = new Vec2(x, y)
    this.radius = this.body.getComponent(UITransform).contentSize.x / 2
    this.node.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
    this.node.on(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
    this.node.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
    this.node.on(Input.EventType.TOUCH_END, this.onTouchEnd, this)
  }

  onDestroy() {
    this.node.off(Input.EventType.TOUCH_START, this.onTouchStart, this)
    this.node.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this)
    this.node.off(Input.EventType.TOUCH_END, this.onTouchEnd, this)
    this.node.off(Input.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
  }

  onTouchStart(event: EventTouch) {
    if (DataManager.Instance.drawerNumber != 0) return
    this.unschedule(this.callback)
    const touchPos = event.getUILocation()
    this.body.setPosition(touchPos.x, touchPos.y)
  }

  onTouchMove(event: EventTouch) {
    if (this.timeout) return
    this.timeout = setTimeout(res => {
      clearTimeout(this.timeout)
      this.timeout = null
    }, 150)
    const touchPos = event.getUILocation()
    const stickPos = new Vec2(touchPos.x - this.body.position.x, touchPos.y - this.body.position.y)
    let angle = Math.atan2(stickPos.y, stickPos.x);
    angle = (angle * 180 / Math.PI + 90 + 360) % 360;
    let direction = '';
    let status = ''
    if (angle >= 45 && angle <= 135) {
      direction = DIRECTION_ENUM.RIGHT;
      status = 'right';
    } else if (angle > 135 && angle <= 225) {
      direction = DIRECTION_ENUM.TOP;
      status = 'top';
    } else if (angle > 225 && angle <= 315) {
      direction = DIRECTION_ENUM.LEFT;
      status = 'left';
    } else {
      direction = DIRECTION_ENUM.BOTTOM;
      status = 'bottom';
    }
    if (stickPos.length() > this.radius) {
      stickPos.multiplyScalar(this.radius / stickPos.length())
    }
    tween(this.stick)
      .to(.2, { position: new Vec3(stickPos.x, stickPos.y, 0) })
      .start()
    if (DataManager.Instance.gameMode == GAME_MODE_ENUM.ONLINE) {
      if (userStore()?.info) {
        sendSocket(SOCKET_TYPE.JOYSTICK, {
          name: DataManager.Instance.current_role,
          status
        })
      }
    } else {
      EventManager.Instance.emit("setStatus", status)
    }
    this.status = status
  }
  onTouchEnd() {
    this.body.setPosition(this.defaultPos.x, this.defaultPos.y, 1)
    if (DataManager.Instance.gameMode == GAME_MODE_ENUM.ONLINE) {
      console.log(DataManager.Instance.room.is_owner)
      sendSocket(SOCKET_TYPE.JOYSTICKEND, {
        name: DataManager.Instance.current_role,
        status: this.status
      })
    } else {
      EventManager.Instance.emit("setStatusEnd", this.status);
    }
    this.stick.setPosition(0, 0)
    this.status = ""
  }
}
